﻿module Program
open System
open System.Collections.Generic
open System.IO
open MathNet.Numerics
open MathNet.Numerics.FSharp
open MathNet.Numerics.LinearAlgebra.Double
open System.Drawing
open System.Windows.Forms
open Types
open Utils
open PCA

//lettura dei dati da file
let stream = File.Open("..\..\data\semeion.data",FileMode.Open, FileAccess.Read)
let stream_reader = new StreamReader(stream) 
let text = stream_reader.ReadToEnd()
stream_reader.Close()
stream.Close()


let labels = Map.ofList([("Iris-setosa",0.); ("Iris-versicolor",1.); ("Iris-virginica",2.)])
let k = 10

let input =

    let splitted_text = text.Split('\n')  
                        |> Seq.filter(fun elem -> elem <> "") 
                        |> Seq.toList

    let rec build_vectors (list : (int*string) list) =
      match list with
      []  -> []
      |(i,x)::xs -> 
        let first_266 = x.Split(' ', '\r')  
                            |> Seq.filter(fun elem -> elem <> "") 
                            |> Seq.map(fun p -> float p) 
                            
        let first_256 = first_266 |> Seq.take 256 |> Seq.toArray
        let real_number   = first_266 |> Seq.skip 256 |> Seq.mapi(fun i p -> if float p = 1.0 then i else 0) |> Seq.sum


        let points = new DenseVector(first_256)

        DenseVectorWrapper(points, i, real_number, -1 |> ref, DenseVector(k)) :: build_vectors xs
        
    

    List(build_vectors (splitted_text |> List.mapi(fun i s -> (i,s))))

let seed = Random()
let dimension = 10

let splitted_classes() =
  [for i in 0 .. 9 do
    yield List(input |> Seq.filter(fun p -> p.RealNumber = i))]
    
let get_elements() =
  let splitted_classes = splitted_classes()
  let train_set=
    [for input_set in splitted_classes do
      for _ in 1 .. dimension do
        let index =  seed.Next(0, input_set.Count)
        let elem = input_set.[index]
        input_set.RemoveAt(index)
        yield elem]
  List(train_set), List([for input_set in splitted_classes do yield! input_set])


do main get_elements
